<template>
  <div>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 搜索框 与 新增框 表单-->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="审批人">
          <el-input
            placeholder="查询供应商名"
            style="width: 300px"
            v-model="msg1"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="margin-left: 20px"
            @click="handlesearch"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 订单信息列表 -->
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="id" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <!-- 订单价格得通过商品表和规格表进行关联 -->
      <el-table-column label="供应商名" width="260">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="商家名" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.merchant.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createdTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.msg }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        :current-page="pager.currentPage"
        :page-size="pager.pageSize"
        layout="prev, pager, next, jumper,sizes, total"
        :total="pager.totalSize"
        :page-sizes="[5, 10, 15, 20]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getlist, getByName } from "../api/supplier";
export default {
  data() {
    return {
      msg1:'',
      currentIndex: 0,
      pager: {
        //每页显示条目个数
        pageSize: 5,
        currentPage: 1,
        totalSize: 0,
      },
      tableData: [],
    };
  },
  methods: {
    //拉取数据
    fetchData() {
      getlist(this.pager)
        .then((res) => {
          console.log(res);
          this.tableData = res.data.data;
          this.pager = res.data.pager;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //查询供应商
    handlesearch() {
       if (this.msg1 && this.msg1.length > 0) {
        getByName({ data: this.msg1 })
          .then((data) => {
            if (data.code === 1000) {
              //往空数组里放对象的属性值
              this.tableData = data.data;
              console.log(this.tableData);
              this.pager.totalSize = this.tableData.length;
            } else {
              this.tableData = data.data.data;
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.fetchData();
      }
    },
    // 通过页码来拉取数据
    handleCurrentChange(val) {
      this.pager.currentPage = val;
      this.fetchData();
    },
    // 改变页大小来拉取数据
    handleSizeChange(val) {
      this.pager.pageSize = val;
      this.fetchData();
    },
  },
  created() {
    this.fetchData();
  },
};
</script>